/**
 * 
 */
package com.example.speedwaymanager;

import junit.framework.Assert;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @author lnowak
 *
 */
public class TableFragment extends Fragment {
	
	public static final String ARG_OBJECT = "object";
	CGameManager gm = CGameManager.getInstance();
	View rootView;
	private TextView tv;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {            
		// 	Inflate the layout for this fragment             
       rootView = inflater.inflate(R.layout.table_fragment, container, false);
       Bundle args = getArguments();
       //((TextView) rootView.findViewById(R.id.textView_teamName)).setText(Integer.toString(args.getInt(ARG_OBJECT)));
       initView(args.getInt(ARG_OBJECT));
       CTeam team = gm.get_match().get_teams().get(args.getInt(ARG_OBJECT));
       rootView.setBackgroundResource(getDrawable(getActivity(), team.get_resourceName()));
       return rootView;        
    }
	
	public static int getDrawable(Context context, String name) {    
        Assert.assertNotNull(context);
        Assert.assertNotNull(name);

        return context.getResources().getIdentifier(name,"drawable", context.getPackageName());
    }
	
	private void initView(int i)
	{
		CTeam team = gm.get_match().get_teams().get(i);
		
		// numbers
		tv = (TextView) rootView.findViewById(R.id.text_tNo1);
		tv.setText(team.get_squad().get(0).get_number().toString() + ".");
		tv = (TextView) rootView.findViewById(R.id.text_tNo2);
		tv.setText(team.get_squad().get(1).get_number().toString() + ".");
		tv = (TextView) rootView.findViewById(R.id.text_tNo3);
		tv.setText(team.get_squad().get(2).get_number().toString() + ".");
		tv = (TextView) rootView.findViewById(R.id.text_tNo4);
		tv.setText(team.get_squad().get(3).get_number().toString() + ".");
		tv = (TextView) rootView.findViewById(R.id.text_tNo5);
		tv.setText(team.get_squad().get(4).get_number().toString() + ".");
		tv = (TextView) rootView.findViewById(R.id.text_tNo6);
		tv.setText(team.get_squad().get(5).get_number().toString() + ".");
		tv = (TextView) rootView.findViewById(R.id.text_tNo7);
		tv.setText(team.get_squad().get(6).get_number().toString() + ".");
		//riders
		tv = (TextView) rootView.findViewById(R.id.textView_rider1);
		tv.setText(team.get_squad().get(0).get_name());
		tv = (TextView) rootView.findViewById(R.id.textView_rider2);
		tv.setText(team.get_squad().get(1).get_name());
		tv = (TextView) rootView.findViewById(R.id.textView_rider3);
		tv.setText(team.get_squad().get(2).get_name());
		tv = (TextView) rootView.findViewById(R.id.textView_rider4);
		tv.setText(team.get_squad().get(3).get_name());
		tv = (TextView) rootView.findViewById(R.id.textView_rider5);
		tv.setText(team.get_squad().get(4).get_name());
		tv = (TextView) rootView.findViewById(R.id.textView_rider6);
		tv.setText(team.get_squad().get(5).get_name());
		tv = (TextView) rootView.findViewById(R.id.textView_rider7);
		tv.setText(team.get_squad().get(6).get_name());	
		// points
		tv = (TextView) rootView.findViewById(R.id.textView_tPts1);
		tv.setText(team.get_squad().get(0).get_matchStats().getStringPoints());
		tv = (TextView) rootView.findViewById(R.id.textView_tPts2);
		tv.setText(team.get_squad().get(1).get_matchStats().getStringPoints());
		tv = (TextView) rootView.findViewById(R.id.textView_tPts3);
		tv.setText(team.get_squad().get(2).get_matchStats().getStringPoints());
		tv = (TextView) rootView.findViewById(R.id.textView_tPts4);
		tv.setText(team.get_squad().get(3).get_matchStats().getStringPoints());
		tv = (TextView) rootView.findViewById(R.id.textView_tPts5);
		tv.setText(team.get_squad().get(4).get_matchStats().getStringPoints());
		tv = (TextView) rootView.findViewById(R.id.textView_tPts6);
		tv.setText(team.get_squad().get(5).get_matchStats().getStringPoints());
		tv = (TextView) rootView.findViewById(R.id.textView_tPts7);
		tv.setText(team.get_squad().get(6).get_matchStats().getStringPoints());
		//sum		
		tv = (TextView) rootView.findViewById(R.id.textView_tSum1);
		tv.setText(team.get_squad().get(0).get_matchStats().get_points().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tSum2);
		tv.setText(team.get_squad().get(1).get_matchStats().get_points().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tSum3);
		tv.setText(team.get_squad().get(2).get_matchStats().get_points().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tSum4);
		tv.setText(team.get_squad().get(3).get_matchStats().get_points().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tSum5);
		tv.setText(team.get_squad().get(4).get_matchStats().get_points().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tSum6);
		tv.setText(team.get_squad().get(5).get_matchStats().get_points().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tSum7);
		tv.setText(team.get_squad().get(6).get_matchStats().get_points().toString());
		//bonuses
		tv = (TextView) rootView.findViewById(R.id.textView_tBon1);
		tv.setText(team.get_squad().get(0).get_matchStats().get_bonuses().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tBon2);
		tv.setText(team.get_squad().get(1).get_matchStats().get_bonuses().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tBon3);
		tv.setText(team.get_squad().get(2).get_matchStats().get_bonuses().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tBon4);
		tv.setText(team.get_squad().get(3).get_matchStats().get_bonuses().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tBon5);
		tv.setText(team.get_squad().get(4).get_matchStats().get_bonuses().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tBon6);
		tv.setText(team.get_squad().get(5).get_matchStats().get_bonuses().toString());
		tv = (TextView) rootView.findViewById(R.id.textView_tBon7);
		tv.setText(team.get_squad().get(6).get_matchStats().get_bonuses().toString());		
		//score
		tv = (TextView) rootView.findViewById(R.id.textView_score);
		tv.setText(	gm.get_match().get_teams().get(0).get_name() + "  " + 	
					gm.get_match().get_teams().get(0).get_points() + " - " +
					gm.get_match().get_teams().get(1).get_points() + "  " +
					gm.get_match().get_teams().get(1).get_name() );
		}
}
